package com.xb.test.utils;

import com.xb.test.entity.Browser;
import com.xb.test.parser.BrowserParser;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

public class BrowserUtils {

    private final static String CONFIG_FILE = "/BrowserConfig.yaml";
    private static BrowserParser parser;

    static{
        Yaml yaml = new Yaml(new SafeConstructor());
        try (InputStream stream = Browser.class.getResourceAsStream(CONFIG_FILE)) {
            List<Map<String, String>> props = (List<Map<String, String>>) yaml.load(stream);
            parser = BrowserParser.fromList(props);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static Browser getBrowser(String ua){
        Browser browser = parser.parse(ua);
        return browser;
    }


//    public static void main(String[] args) {
//        String ua = "Mozilla/5.0 (Linux; U; Android 8.1.0; zh-cn; PACM00 Build/O11019) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/53.0.2785.134 Mobile Safari/537.36 OppoBrowser/4.6.5.3";
//        Browser browser = BrowserUtils.getBrowser(ua);
//        String brand = browser.brand;
//        String family = browser.family;
//        String major = browser.major;
//        String minor = browser.minor;
//        System.out.println(brand);
//        System.out.println(family);
//        System.out.println(major);
//        System.out.println(minor);
//    }
}
